package com.tcc.fastline;

import com.tcc.fastline.Fragments.Factory.FragmentFactory;
import com.tcc.fastline.Helpers.BuilderHelper;
import com.tcc.fastline.Helpers.MenuHelper;
import com.tcc.fastline.Helpers.NavigationHelper;
import com.tcc.smartqueue.R;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FastLineActivity extends Activity
{
	SessionManager session;

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] menu;

	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smart_queue);

		IniciarSessaoEVerificarSeEstaLogado();
		IniciarVariaveisAplicacao();

		AdicionarFragmentTelaInicio();

		iniciarNavigationDrawer();
	}

	private void IniciarSessaoEVerificarSeEstaLogado()
	{
		session = new SessionManager(getApplicationContext());
		session.checkLogin();
	}

	private void IniciarVariaveisAplicacao()
	{
		FragmentFactory.CarregarCatalago();
		NavigationHelper.manager = getFragmentManager();
		MenuHelper.IniciarMenu(session.getTipoUsuario());
	}

	private void AdicionarFragmentTelaInicio()
	{
		getFragmentManager().beginTransaction().add(R.id.conteudo, new PlaceholderFragment(R.layout.inicio))
				.commit();
	}

	private void iniciarNavigationDrawer()
	{
		mTitle = mDrawerTitle = getTitle();
		menu = getResources().getStringArray(getArrayMenu());
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerLayout.setDrawerShadow(R.drawable.ic_drawer, GravityCompat.START);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menu));
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.drawer_shadow,
				R.string.drawer_open, R.string.drawer_close)
		{
			public void onDrawerClosed(View view)
			{
				getActionBar().setTitle(mTitle);
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView)
			{
				getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}

	private int getArrayMenu()
	{
		String tipo = session.getTipoUsuario();

		if (tipo.toUpperCase().equals("ESTABELECIMENTO"))
		{
			return R.array.Menu_Estabelecimentos;
		}
		else if (tipo.toUpperCase().equals("USUARIO"))
		{
			return R.array.Menu_Usuarios;
		}

		return 0;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.login, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (mDrawerToggle.onOptionsItemSelected(item))
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class DrawerItemClickListener implements ListView.OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			selectItem(position);
		}

	}

	private void selectItem(int position)
	{
		NavegarTela(position);

		mDrawerList.setItemChecked(position, true);
		setTitle(menu[position]);
		mDrawerLayout.closeDrawer(mDrawerList);
	}

	private void NavegarTela(int posicaoMenu)
	{
		String option = "menu_" + session.getTipoUsuario() + "_" + posicaoMenu;
		MenuHelper.NavegarMenu(option.toUpperCase());
	}

	@Override
	public void setTitle(CharSequence title)
	{
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState)
	{
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
		NavigationHelper.exibirPagina(R.layout.inicio);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public void onBackPressed()
	{
		NavigationHelper.exibirPagina(R.layout.inicio);
	}

	private void ExibirValidationServerException(String mensagem)
	{
		BuilderHelper.OK("Erro", mensagem, context, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{

			}
		});
	}
}
